//
//  AppDelegate.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/4.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
//        printfLog(NSBundle.mainBundle().infoDictionary)
        
        //初始化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: MQSwitchRootViewControllerNotification, object: nil)
        //设置根控制器
        window?.rootViewController = MQUserAccountViewModel.shareUserAccountViewModel.isUserLogin ? defaultRootViewController() : MQMainViewController()
        
        //设置window为主界面并且可见
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    @objc private func switchRootViewController(noti:NSNotification){
        if noti.object == nil{
            window?.rootViewController = MQMainViewController()
        }else{
            window?.rootViewController = MQWelcomeViewController()
        }
    }
    
    private func defaultRootViewController() -> UIViewController{
        if hasNewVersion(){
            return MQNewFeatureViewController()
        }else{
            return MQWelcomeViewController()
        }
    }
    
    private func hasNewVersion() -> Bool{
        
        //从沙盒获取存的版本号
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey("version")
        //当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //存储当前版本号
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "version")
        if sandBoxVersion == nil{
            return true
        }
        
        if currentVersion.compare(sandBoxVersion!) == .OrderedDescending{
            return true
        }
        
        return false
    }
}

